﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevelopStudio.Basic;
using SNSHelper.Common;
using CommonBasic = DevelopStudio.Common.Basic;
using CommonManage = DevelopStudio.Common.Manage;
using DevelopStudio.Control.Basic;

namespace DevelopStudio.Control
{
    public partial class RedminePageControl : PageControl
    {
        private RedmineInfo tempRedmineInfo;

        private bool signIn = false;

        public RedminePageControl()
        {
            InitializeComponent();
        }

        public override void InitializePageControl()
        {
            base.InitializePageControl();

            tempRedmineInfo = new RedmineInfo();

            if (this.PageData.Redmine.BasicInfo != null)
            {
                this.tempRedmineInfo.BasicInfo = this.PageData.Redmine.BasicInfo.Clone() as BasicRedmineInfo;
            }
            else
            {
                this.tempRedmineInfo = new RedmineInfo();
            }

            this.textBoxWebsite.Text = this.tempRedmineInfo.BasicInfo.BaseUrl;
            this.textBoxName.Text = this.tempRedmineInfo.BasicInfo.UserName;
            this.textBoxPassword.Text = this.tempRedmineInfo.BasicInfo.Password;
            this.listView1.Items.Clear();

            foreach (MemberInfo var in this.PageData.Redmine.Members)
            {
                this.listView1.Items.Add(new ListViewItem(new string[] { var.ID, var.Name, var.Email }));
            }

            this.Initialized = true;
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            if (this.Initialized == false)
            {
                return;
            }

            if (this.ValidateLoginInfo() == false)
            {
                CommonManage.Utility.ShowMessage(this.labelMessage, Properties.Resources.ErrorLoginInformation, CommonBasic.MessageLevel.Warning);
                return;
            }

            this.labelMessage.Text = string.Empty;
            // Clear the login buffer.
            Utility.Logout();
            this.signIn = false;
            string message;
            if (Utility.Login(out message, this.tempRedmineInfo) == false)
            {
                CommonManage.Utility.ShowMessage(this.labelMessage, message, CommonBasic.MessageLevel.Critical);
            }
            else
            {
                CommonManage.Utility.ShowMessage(this.labelMessage, message, CommonBasic.MessageLevel.Normal);
                this.signIn = true;
            }
        }


        private bool ValidateLoginInfo()
        {
            if (this.textBoxWebsite.Text ==  PageData.Redmine.BasicInfo.BaseUrl &&
                this.textBoxName.Text == PageData.Redmine.BasicInfo.UserName &&
                this.textBoxPassword.Text == PageData.Redmine.BasicInfo.Password)
            {
                return true;
            }

            if (string.IsNullOrEmpty(this.textBoxWebsite.Text) == true ||
                string.IsNullOrEmpty(this.textBoxName.Text) == true ||
                string.IsNullOrEmpty(this.textBoxPassword.Text) == true)
            {
                return false;
            }

            return true;
        }

        public override bool ValidatePage()
        {
            if (base.ValidatePage() == false)
            {
                return false;
            }

            if (this.ValidateLoginInfo() == false)
            {
                MessageBox.Show(Properties.Resources.ErrorLoginInformation, this.ParentForm.Text);
                return false;
            }

            return true;
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (this.Initialized == false)
            {
                return;
            }

            this.LoadMembers();
        }

        private void LoadMembers()
        {
            if (this.signIn == false)
            {
                string message;
                if (Utility.Login(out message, this.tempRedmineInfo) == false)
                {
                    CommonManage.Utility.ShowMessage(this.labelMessage, message, CommonBasic.MessageLevel.Critical);
                    return;
                }
                else
                {
                    CommonManage.Utility.ShowMessage(this.labelMessage, message, CommonBasic.MessageLevel.Normal);
                    this.signIn = true;
                }
            }

            Utility.GetMembersInfo(this.tempRedmineInfo);

            this.listView1.Items.Clear();

            foreach (MemberInfo var in this.tempRedmineInfo.Members)
            {
                this.listView1.Items.Add(new ListViewItem(new string[] { var.ID, var.Name }));
            }
        }

        public override void ApplyChanges()
        {
            base.ApplyChanges();

            this.PageData.Redmine.BasicInfo = this.tempRedmineInfo.BasicInfo.Clone() as BasicRedmineInfo;
            this.PageData.Redmine.Projects.Clear();
            this.PageData.Redmine.Projects.AddRange(this.tempRedmineInfo.Projects);

            if (this.listView1.Items.Count <= 0)
            {
                this.LoadMembers();
            }

            if (this.listView1.Items.Count > 0)
            {
                this.PageData.Redmine.Members.ForEach(o => o.Enabled = false);

                foreach (ListViewItem var in this.listView1.Items)
                {
                    MemberInfo member = this.PageData.Redmine.Members.Find(o => o.ID == var.SubItems[0].Text);

                    if (member == null)
                    {
                        member = new MemberInfo() { ID = var.SubItems[0].Text, Name = var.SubItems[1].Text };
                        this.PageData.Redmine.Members.Add(member);
                    }
                    else
                    {
                        member.Name = var.SubItems[1].Text;
                    }

                    member.Enabled = true;
                }

                List<MemberInfo> removedMembers = this.PageData.Redmine.Members.Where(o => o.Enabled == false).ToList();

                foreach (MemberInfo var in removedMembers)
                {
                    this.PageData.Redmine.Members.Remove(var);
                }
            }
        }

        private void textBoxWebsite_TextChanged(object sender, EventArgs e)
        {
            this.tempRedmineInfo.BasicInfo.BaseUrl = this.textBoxWebsite.Text.TrimEnd('/');
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            this.tempRedmineInfo.BasicInfo.UserName = this.textBoxName.Text;
        }

        private void textBoxPassword_TextChanged(object sender, EventArgs e)
        {
            this.tempRedmineInfo.BasicInfo.Password = this.textBoxPassword.Text;
        }
    }
}
